package org.example.batisDemo;

import com.sun.istack.internal.Nullable;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.mapper.FqmMapper;
import org.example.mapper.FqmUserMapper;
import org.example.mapper.JJMapper;
import org.example.pojo.Fqm;
import org.example.pojo.FqmUser;
import org.example.pojo.FqmUserFlag;
import org.example.pojo.JJ;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

// myBatis代理开发
public class MyBatisDemo2 {

    private static final String RESOURCE = "mybatis-demo2-config.xml";
    private static SqlSession SqlSession;

    private void loadSession() throws IOException {
        // 加载mybatis配置文件
        InputStream inputStream = Resources.getResourceAsStream(RESOURCE);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获取sqlSession 对象，可以执行sql语句
        // 传入的参数为true，表示自动提交，默认为false，需要手动提交事务
        MyBatisDemo2.SqlSession = sqlSessionFactory.openSession(true);
        System.out.println("加载session成功");
    }

    public MyBatisDemo2() {
        try {
            loadSession();
        } catch (IOException e) {
            System.out.println("加载session失败");
            e.printStackTrace();
        }
    }


    public List<Fqm> queryAll() {
        // 注意这里通过getMapper方法获取mapper接口的实现类：实现是由 SqlSessionFactoryBuilder.build(inputStream) 创建的SqlSession对象，内部实现的是MapperProxy
        // 我们只需要用即可，不需要自己实现
        FqmMapper mapper = SqlSession.getMapper(FqmMapper.class);
        List<Fqm> fqmList = mapper.selectAll();
        System.out.println(fqmList);
        return fqmList;
    }

    public Fqm queryById(Integer id) {
        FqmMapper mapper = SqlSession.getMapper(FqmMapper.class);
        Fqm fqm = mapper.selectById(id);
        System.out.println(fqm);
        return fqm;
    }

    public List<JJ> queryAllJJ() {
        JJMapper mapper = SqlSession.getMapper(JJMapper.class);
        List<JJ> GODS = mapper.selectAll();
        System.out.println(GODS);
        return GODS;
    }

    public JJ queryJJById(Integer id) {
        JJMapper mapper = SqlSession.getMapper(JJMapper.class);
        JJ jj = mapper.selectById(id);
        System.out.println(jj);
        return jj;
    }

    public List<FqmUser> queryAllFqmUser() {
        FqmUserMapper mapper = SqlSession.getMapper(FqmUserMapper.class);
        List<FqmUser> fqmUsers = mapper.queryAll();
        System.out.println(fqmUsers);
        return fqmUsers;
    }

    public List<FqmUser> queryFqmUserByName(String name) {
        FqmUserMapper mapper = SqlSession.getMapper(FqmUserMapper.class);
        List<FqmUser> fqmUsers = mapper.queryFqmUserByName(name);
        System.out.println(fqmUsers);
        return fqmUsers;
    }

    public FqmUser queryFqmUserById(int id) {
        FqmUserMapper mapper = SqlSession.getMapper(FqmUserMapper.class);
        FqmUser fqmUser = mapper.queryFqmUserById(id);
        System.out.println(fqmUser);
        return fqmUser;
    }

    public List<FqmUser> queryFqmUserByNameOrAddress(@Nullable String name, @Nullable String address) {
        FqmUserMapper mapper = SqlSession.getMapper(FqmUserMapper.class);
        // 故意给反顺序的参数，看下@param注解的效果
        List<FqmUser> fqmUsers = mapper.queryFqmUserByNameOrAddress(address, name);
        System.out.println(fqmUsers);
        return fqmUsers;
    }

    public List<FqmUser> queryFqmUserByNameOrAddress_no_at_param(@Nullable String name, @Nullable String address) {
        FqmUserMapper mapper = SqlSession.getMapper(FqmUserMapper.class);
        List<FqmUser> fqmUsers = mapper.queryFqmUserByNameOrAddress_no_at_param(address, name);
        System.out.println(fqmUsers);
        return fqmUsers;
    }

    public List<FqmUser> queryMutipart(FqmUserFlag fqmUserFlag) {
        FqmUserMapper mapper = SqlSession.getMapper(FqmUserMapper.class);
        List<FqmUser> fqmUsers = mapper.queryMutipart(fqmUserFlag);
        System.out.println(fqmUsers);
        return fqmUsers;
    }

    public int updateFqmUser(FqmUser fqmUser) {
        FqmUserMapper mapper = SqlSession.getMapper(FqmUserMapper.class);
        int i = mapper.updateFqmUser(fqmUser);
        System.out.println("更新了" + i + "条数据");
        // 注意，默认的mybatis的配置是设置autoCommit=false，所以需要手动提交
        // 可以通过SqlSessionFactory.openSession(true)方法初始化，设置autoCommit=true，这样就不用手动提交了
        // SqlSession.commit();
        return i;
    }

    public int insertFqmUser(FqmUser fqmUser) {
        FqmUserMapper mapper = SqlSession.getMapper(FqmUserMapper.class);
        int i = mapper.insertFqmUser(fqmUser);
        System.out.println("插入了" + i + "条数据");
        return i;
    }

    public int deleteFqmUserById(int id) {
        FqmUserMapper mapper = SqlSession.getMapper(FqmUserMapper.class);
        int i = mapper.deleteFqmUserById(id);
        System.out.println("删除了" + i + "条数据");
        return i;
    }

    public int deleteFqmUserByIds(Integer[] ids) {
        FqmUserMapper mapper = SqlSession.getMapper(FqmUserMapper.class);
        int i = mapper.deleteFqmUserByIds(ids);
        System.out.println("删除了" + i + "条数据");
        return i;
    }

    public <T> int deleteFqmUserByNameOrId(T idOrName) {
        FqmUserMapper mapper = SqlSession.getMapper(FqmUserMapper.class);
        int i = mapper.deleteFqmUserByIdOrName(idOrName);
        System.out.println("删除了" + i + "条数据");
        return i;
    }

    //     关闭session
    public static void close() {
        if (SqlSession != null) {
            SqlSession.close();
            System.out.println("关闭session成功");
        }
    }

}
